local controls_sel i.age_bin##female
local controls_out i.age_bin##female i.years_in_us_bin##female

** First, run analysis that requires surveys after 2011, or use a different sample than main sample, so that we can drop unused vars afterward for faster runtime

*** Fig B5 -- Trajectories
use "$path\Tempfiles\USCensus_vars.dta", clear
drop if sample_traj == . //improve runtime
svyset cluster [pweight=perwt_full], strata(new_strata) singleunit(scaled) fpc(sampling_rate)

local append_replace replace
local iter = 0

foreach outcome in employed high_occ high_med_occ log_income Eng_well{
local iter = `iter' + 1

if `iter' == 2{
local append_replace append
}

svy, subpop(sample_traj): reg `outcome' EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80 Imm_btw_95_00_y17 Imm_btw_95_00_y10 Imm_btw_95_00_y00 Imm_btw_87_90_y10 Imm_btw_87_90_y00 Imm_btw_87_90_y90 Imm_btw_75_80_y00 Imm_btw_75_80_y90 Imm_btw_75_80_y80 i.birth_country i.year `controls_out'

outreg2 using "$path\Output\US\figb5_us.xls", `append_replace' label stats(coef se) nonotes sortvar(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80) keep(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80)
}

cap erase "$path\Output\US\figb5_us.txt"

*** Fig D2 -- Differential attrition
svy, subpop(sample_traj): reg age_alt EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80 Imm_btw_95_00_y17 Imm_btw_95_00_y10 Imm_btw_95_00_y00 Imm_btw_87_90_y10 Imm_btw_87_90_y00 Imm_btw_87_90_y90 Imm_btw_75_80_y00 Imm_btw_75_80_y90 Imm_btw_75_80_y80 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\figd2_us.xls", replace label stats(coef se) nonotes sortvar(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80) keep(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80)

svy, subpop(sample_traj): reg female EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80 Imm_btw_95_00_y17 Imm_btw_95_00_y10 Imm_btw_95_00_y00 Imm_btw_87_90_y10 Imm_btw_87_90_y00 Imm_btw_87_90_y90 Imm_btw_75_80_y00 Imm_btw_75_80_y90 Imm_btw_75_80_y80 i.birth_country i.year i.age_bin
outreg2 using "$path\Output\US\figd2_us.xls", append label stats(coef se) nonotes sortvar(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80) keep(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80)

svy, subpop(sample_traj if Imm == 1): reg immigration_year EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 Imm_btw_95_00_y17 Imm_btw_95_00_y10 Imm_btw_95_00_y00 Imm_btw_87_90_y10 Imm_btw_87_90_y00 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\figd2_us.xls", append label stats(coef se) nonotes sortvar(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80) keep(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80)

svy, subpop(sample_traj): reg edu_years EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80 Imm_btw_95_00_y17 Imm_btw_95_00_y10 Imm_btw_95_00_y00 Imm_btw_87_90_y10 Imm_btw_87_90_y00 Imm_btw_87_90_y90 Imm_btw_75_80_y00 Imm_btw_75_80_y90 Imm_btw_75_80_y80 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\figd2_us.xls", append label stats(coef se) nonotes sortvar(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80) keep(EB_btw_95_00_y17 EB_btw_95_00_y10 EB_btw_95_00_y00 EB_btw_87_90_y10 EB_btw_87_90_y00 EB_btw_87_90_y90 EB_btw_75_80_y00 EB_btw_75_80_y90 EB_btw_75_80_y80)

cap erase "$path\Output\US\figd2_us.txt"

*** Fig 3 (Selection from origin using Bayes' rule) + Fig B2 + Fig B3
use "$path\Tempfiles\USCensus_vars.dta", clear
keep if inlist(year,2009,2010,2011,2012,2013) // create 5% sample around 2011
keep if EB==1
cd "$path\Output\US"

svyset cluster [pweight=perwt_2011], strata(new_strata) singleunit(scaled) fpc(sampling_rate)

* Poland
putexcel set us_poland_all, replace

svy: tab isced_int if bpl == 455 & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2011 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if bpl == 455 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if bpl == 455 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_int if bpl == 455 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if bpl == 455 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_int if bpl == 455 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_int if bpl == 455 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2011  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Hungary
putexcel set us_hungary_all, replace

svy: tab isced_int if bpl == 454 & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2011 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if bpl == 454 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if bpl == 454 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_int if bpl == 454 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if bpl == 454 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_int if bpl == 454 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_int if bpl == 454 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2011  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Romania
putexcel set us_romania_all, replace

svy: tab isced_int if bpl == 456  & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2011 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if bpl == 456 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if bpl == 456  & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_int if bpl == 456 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if bpl == 456  & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_int if bpl == 456 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_int if bpl == 456  & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2011  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Romanian Jews
putexcel set us_romania_jews, replace

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2011 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2011  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)


* Romanian Aussiedler
putexcel set us_romania_germans, replace

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2011 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2011 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_int if bpl == 456 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2011  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)


* Russia 2010 census
putexcel set us_russia_all, replace

svy: tab isced_rus if bpld == 46500 & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2010 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_rus if bpld == 46500 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_rus if bpld == 46500 & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_rus if bpld == 46500 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_rus if bpld == 46500 & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_rus if bpld == 46500 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_rus if bpld == 46500 & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2010  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Russia 2010 census -- Germans, include whole USSR
putexcel set us_russia_germans, replace

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2010 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 32 | ancestr2 == 32) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2010  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Russia 2010 census -- Jews, include whole USSR
putexcel set us_russia_jews, replace

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1946 & birthyr <= 1986 & immigration_age >= 25 & immigration_year <= 2010 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1925 & birthyr <= 1945 & immigration_year >= 1970 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1942 & birthyr <= 1962 & immigration_year >= 1987 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

svy: tab isced_rus if Sov == 1 & (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) & birthyr >= 1948 & birthyr <= 1968 & immigration_year >= 1993 & immigration_year <= 2010  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)



*** Table B5 Sensitivity analysis
use "$path\Tempfiles\USCensus_vars.dta", clear
drop if year > 2011
svyset cluster [pweight=perwt_main], strata(new_strata) singleunit(scaled) fpc(sampling_rate)

gen middle = (immigration_year>=1980 & immigration_year<=1999) // to focus on middle period
gen exclude_35 = Imm == 1 & immigration_age < 35 //to exclude those arriving before 35
gen exclude_jewish = (ancestr1 == 419 | ancestr2 == 419 | inlist(language,3,59)) //to exclude jewish immigrants
gen sample_recent_5 = sample
replace sample_recent_5 = 0 if Imm == 1 & (immigration_year >= year - 5 & immigration_year != .) 

**Construct linear pre-period trend
gen trend = (immigration_year_approx - 1986) * post_87
replace trend = 0 if Imm == 0
gen EB_trend = EB * (immigration_year_approx - 1960) if Imm == 1
gen WE_trend = (1 - EB) * (immigration_year_approx - 1960) if Imm == 1
gen prtt_edu_years = 0
reg edu_years EB_trend WE_trend i.birth_country `controls_sel' [pweight = perwt_main] if inrange(immigration_year,1961,1986) & sample == 1
replace prtt_edu_years = _b[EB_trend] * trend if post_87 == 1 & EB == 1
replace prtt_edu_years = _b[WE_trend] * trend if post_87 == 1 & EB == 0
drop EB_trend WE_trend trend

*Regs
svy: mean edu_years if sample==1 & EB==1 & middle == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if middle==1 | native==1): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", replace label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy: mean edu_years if sample==1 & EB==1 & exclude_35 != 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if exclude_35!=1): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy: mean edu_years if sample==1 & EB==1 & exclude_jewish != 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if exclude_jewish!=1): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy: mean edu_years if sample2 == 1 & EB == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample2): reg edu_years EB_post_93 EB_post_87 Imm2_post_93 Imm2_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm2_post_93 Imm2_post_87) keep(EB_post_93 EB_post_87 Imm2_post_93 Imm2_post_87)

svy: mean edu_years if sample_allages == 1 & EB == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample_allages): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

*exclude 1990 census in this one since it aggregates 1987-1990
svyset cluster [pweight=perwt_excl_1990], strata(new_strata) singleunit(scaled) fpc(sampling_rate)
svy: mean edu_years if sample==1 & EB==1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if year != 1990): reg edu_years EB_post_93 EB_post_89 Imm_post_93 Imm_post_89 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_89 Imm_post_93 Imm_post_89) keep(EB_post_93 EB_post_89 Imm_post_93 Imm_post_89)
svyset cluster [pweight=perwt_main], strata(new_strata) singleunit(scaled) fpc(sampling_rate)

svy: mean edu_years if sample_recent_5 == 1 & EB == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample_recent_5): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel' prtt_edu_years
outreg2 using "$path\Output\US\tb5_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 prtt_edu_years) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 prtt_edu_years)

drop middle exclude_35 exclude_jewish sample_recent_5 prtt_edu_years

erase "$path\Output\US\tb5_us.txt"

*** Fig B1 -- Coefficients on Edu by Year
svyset cluster [pweight=perwt_excl_1990], strata(new_strata) singleunit(scaled) fpc(sampling_rate)

svy, subpop(sample if immigration_year<=1998 & year != 1990): reg edu_years EB_98 EB_97 EB_96 EB_95 EB_94 EB_93 EB_92 EB_91 EB_90 EB_89 EB_88 EB_87 EB_86 Imm_98 Imm_97 Imm_96 Imm_95 Imm_94 Imm_93 Imm_92 Imm_91 Imm_90 Imm_89 Imm_88 Imm_87 Imm_86 i.birth_country i.year `controls_sel'
capture noisily outreg2 using "$path\Output\US\figb1_us.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_98 EB_97 EB_96 EB_95 EB_94 EB_93 EB_92 EB_91 EB_90 EB_89 EB_88 EB_87 EB_86 Imm_98 Imm_97 Imm_96 Imm_95 Imm_94 Imm_93 Imm_92 Imm_91 Imm_90 Imm_89 Imm_88 Imm_87 Imm_86) keep(EB_98 EB_97 EB_96 EB_95 EB_94 EB_93 EB_92 EB_91 EB_90 EB_89 EB_88 EB_87 EB_86 Imm_98 Imm_97 Imm_96 Imm_95 Imm_94 Imm_93 Imm_92 Imm_91 Imm_90 Imm_89 Imm_88 Imm_87 Imm_86)
capture noisily erase "$path\Output\US\figb1_us.txt"



** From this point on, we can work with a single dataset and drop unused obs
use "$path\Tempfiles\USCensus_vars.dta", clear
drop if sample == .
svyset cluster [pweight=perwt_main], strata(new_strata) singleunit(scaled) fpc(sampling_rate)


*** Table 1 (Educational Attainment)
qui svy: mean edu_years if sample == 1 & EB == 1
local depvar_mean_eb = el(e(b),1,1)

* pre/post, 2 then 3 periods
svy, subpop(sample if max(native,EB)==1): reg edu_years EB_post_93 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\t1_us.xls", replace label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy, subpop(sample if max(native,EB)==1): reg edu_years EB_post_93 EB_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\t1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

* DID vs W Europeans, 2 then 3 periods, w/ and w/o demographic controls
svy, subpop(sample): reg edu_years EB_post_93 Imm_post_93 i.birth_country i.year
outreg2 using "$path\Output\US\t1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy, subpop(sample): reg edu_years EB_post_93 Imm_post_93 i.birth_country i.year `controls_sel' 
outreg2 using "$path\Output\US\t1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year
outreg2 using "$path\Output\US\t1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel' 
outreg2 using "$path\Output\US\t1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

erase "$path\Output\US\t1_us.txt"


*** Table 2 (Labor Market Outcomes)
local iter = 0
local append_replace "replace"
foreach outcome in employed high_occ high_med_occ log_income Eng_well{

local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}

qui svy: mean `outcome' if sample == 1 & EB == 1
local depvar_mean_eb = el(e(b),1,1)

svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_out'

outreg2 using "$path\Output\US\t2_us.xls", `append_replace' label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
}

cap erase "$path\Output\US\t2_us.txt"

*** Table B1 -- sum stats

*pull subpop sizes in 10,000,000s
qui svy, subpop(sample if native == 1): reg female 
	local obs_1 = e(N_sub)/10000000
	local pop_1 = e(N_subpop)/10000000
qui svy, subpop(sample if Imm == 1 & EB == 0): reg female 
	local obs_2 = e(N_sub)/10000000
	local pop_2 = e(N_subpop)/10000000
qui svy, subpop(sample if Sat == 1): reg female 
	local obs_3 = e(N_sub)/10000000
	local pop_3 = e(N_subpop)/10000000
qui svy, subpop(sample if Sov == 1): reg female 
	local obs_4 = e(N_sub)/10000000
	local pop_4 = e(N_subpop)/10000000
	
svy, subpop(sample if native == 1): mean immigration_age female married edu_years isced97_5a employed high_occ Eng_well 
outreg2 using "$path\Output\US\tb1_us.xls", replace label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_1', "Subpop Size (10000000s)", `pop_1') dec(2) adec(4)
svy, subpop(sample if Imm == 1 & EB == 0): mean immigration_age female married edu_years isced97_5a employed high_occ Eng_well 
outreg2 using "$path\Output\US\tb1_us.xls", append label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_2', "Subpop Size (10000000s)", `pop_2') dec(2) adec(4)
svy, subpop(sample if Sat == 1): mean immigration_age female married edu_years isced97_5a employed high_occ Eng_well 
outreg2 using "$path\Output\US\tb1_us.xls", append label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_3', "Subpop Size (10000000s)", `pop_3') dec(2) adec(4)
svy, subpop(sample if Sov == 1): mean immigration_age female married edu_years isced97_5a employed high_occ Eng_well 
outreg2 using "$path\Output\US\tb1_us.xls", append label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_4', "Subpop Size (10000000s)", `pop_4') dec(2) adec(4)
cap erase "$path\Output\US\tb1_us.txt"

*** Table B3 -- Mincer Returns
gen exper = age_alt - edu_years - 6
gen exper2 = exper*exper

local append_replace replace
foreach outcome in employed high_occ high_med_occ log_income {
	svy, subpop(sample if EB == 1): reg `outcome' edu_years exper exper2 female i.year ${countries}
	outreg2 using "$path\Output\US\tb3_us.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(edu_years) keep(edu_years) noaster
	local append_replace append
}
erase "$path\Output\US\tb3_us.txt"
drop exper exper2


*** Fig B4 -- dropping one origin country at a time
*order: romania poland hungary czechoslovakia bulgaria russia
local append_replace replace
foreach country in 456 455 454 452 451 465{
svy, subpop(sample if bpl != `country'): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\figb4_us.xls", `append_replace' label stats(coef se) nonotes dec(3) sortvar(EB_post_93) keep(EB_post_93)
local append_replace append
}
erase "$path\Output\US\figb4_us.txt"


*** Table B4 Different measures of education
local iter = 0
local append_replace "replace"
foreach outcome in isced97_3 isced97_4 isced97_5b isced97_5a{
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}

	svy: mean `outcome' if sample == 1 & EB_post_87 == 1 & EB_post_93 == 0 //want late refugee mean
	local depvar_mean_eb = e(b)[1,1]		
	svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
	outreg2 using "$path\Output\US\tb4_us.xls", `append_replace' label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
}
erase "$path\Output\US\tb4_us.txt"

*** Table B6 Outcomes w/ education as control

*double edu_years so half-integers become integers, for this FE regression
gen edu_years_fe = edu_years*2

local iter = 0
local append_replace "replace"
foreach outcome in employed high_occ high_med_occ log_income Eng_well{
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}
	qui svy: mean `outcome' if sample == 1 & EB == 1
	local depvar_mean_eb = e(b)[1,1]

	svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country `controls_out' i.year i.edu_years_fe

	outreg2 using "$path\Output\US\tb6_us.xls", `append_replace' label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
}
erase "$path\Output\US\tb6_us.txt"

drop edu_years_fe


*** Table B7 -- Sat/Sov separately & excluding recovery
svy: mean edu_years if sample == 1 & Sat == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if Sov == 0): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb7_us.xls", replace label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy: mean edu_years if sample == 1 & Sov == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if Sat == 0): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb7_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy: mean edu_years if sample == 1 & Sat == 1 & immigration_year <= 1999
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if Sov == 0 & (native == 1 | immigration_year <= 1999)): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb7_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

svy: mean edu_years if sample == 1 & Sov == 1 & immigration_year <= 1999
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample if Sat == 0 & (native == 1 | immigration_year <= 1999)): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.year `controls_sel'
outreg2 using "$path\Output\US\tb7_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily erase "$path\Output\US\tb7_us.txt"

*** Table B9 -- Gender
gen EB_post_93_m = EB_post_93 * female==0
gen EB_post_87_m = EB_post_87 * female==0
gen Imm_post_93_m = Imm_post_93 * female==0
gen Imm_post_87_m = Imm_post_87 * female==0
gen EB_m = EB * female==0
gen Imm_m = Imm * female==0

local iter = 0
local append_replace "replace"
foreach outcome in employed high_occ high_med_occ log_income Eng_well{
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}

svy: mean `outcome' if sample == 1 & EB == 1
local depvar_mean_eb = e(b)[1,1]
svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 EB_post_93_m EB_post_87_m EB_m Imm_post_93 Imm_post_87 Imm_post_93_m Imm_post_87_m Imm_m i.birth_country `controls_out' i.year
outreg2 using "$path\Output\US\tb9_us.xls", `append_replace' label stats(coef se) nonotes afmt(g) dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93_m EB_post_87_m EB_post_93 EB_post_87) keep(EB_post_93_m EB_post_87_m EB_post_93 EB_post_87)
}

erase "$path\Output\US\tb9_us.txt"
drop EB_post_93_m EB_post_87_m Imm_post_93_m Imm_post_87_m EB_m Imm_m


*** Table D1 -- restructuring (heterogeneous effects)

* Selection+Outcomes by gender
gen EB_post_93_m = EB_post_93 * female==0
gen EB_post_87_m = EB_post_87 * female==0
gen Imm_post_93_m = Imm_post_93 * female==0
gen Imm_post_87_m = Imm_post_87 * female==0
gen EB_m = EB * female==0
gen Imm_m = Imm * female==0

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 EB_post_93_m EB_post_87_m EB_m Imm_post_93 Imm_post_87 Imm_post_93_m Imm_post_87_m Imm_m i.birth_country `controls_sel' i.year
capture noisily outreg2 using "$path\Output\US\td1_us.xls", replace label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93_m EB_post_87_m EB_post_93 EB_post_87) keep(EB_post_93_m EB_post_87_m EB_post_93 EB_post_87)
drop EB_post_93_m EB_post_87_m Imm_post_93_m Imm_post_87_m EB_m Imm_m

* Selection+Outcomes by age
gen EB_post_93_y = EB_post_93 * (immigration_age<=34)
gen EB_post_87_y = EB_post_87 * (immigration_age<=34)
gen Imm_post_93_y = Imm_post_93 * (immigration_age<=34)
gen Imm_post_87_y = Imm_post_87 * (immigration_age<=34)
gen EB_y = EB * (immigration_age<=34)
gen young = (immigration_age<=34)

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 EB_post_93_y EB_post_87_y EB_y Imm_post_93 Imm_post_87 Imm_post_93_y Imm_post_87_y i.birth_country young `controls_sel' i.year
capture noisily outreg2 using "$path\Output\US\td1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93_y EB_post_87_y EB_post_93 EB_post_87) keep(EB_post_93_y EB_post_87_y EB_post_93 EB_post_87)
drop EB_post_93_y EB_post_87_y Imm_post_93_y Imm_post_87_y EB_y young

* Selection+Outcomes by Sov/Sat
capture noisily svy, subpop(sample): reg edu_years Sov_post_93 Sov_post_87 EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country `controls_sel' i.year
capture noisily outreg2 using "$path\Output\US\td1_us.xls", append label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(Sov_post_93 Sov_post_87 EB_post_93 EB_post_87) keep(Sov_post_93 Sov_post_87 EB_post_93 EB_post_87)

capture noisily erase "$path\Output\US\td1_us.txt"



